#-*- Makefile -*-
# $(R_HOME}/etc/Makeconf

# Hand-edited version for gnuwin32.

ifdef DEBUG
  DLLFLAGS=
  DEBUGFLAG=-gdwarf-2
else
  DLLFLAGS=-s
  DEBUGFLAG=
endif

BINPREF =
IMPDIR = bin

AS = $(BINPREF)as
## formerly used version without -k for front-ends/libproxy.a
DLLTOOL = $(BINPREF)dlltool
## some versions have trouble with paths, so we always need this
DLLTOOLFLAGS = --as $(AS) -k
NM = $(BINPREF)nm
RESCOMP = $(BINPREF)windres
## MAIN_LD needs to be set by the package
LINKER=$(MAIN_LD)

## Things defined in Renviron on Unix
AWK = gawk
EGREP = grep -e
PERL = perl
SED = sed
TAR = tar

## Things defined in MkRules
CAT = cat
CP = cp
FIND = find
LN_S = cp -p
MKDIR = mkdir
RM = rm -f
SORT = sort

GRAPHAPP_LIB = -lRgraphapp
TCL_HOME = $(R_HOME)/Tcl
TCL_VERSION = 85
ZLIB_LIBS = -lRzlib

AR = $(BINPREF)ar
BLAS_LIBS = -L"$(R_HOME)/$(IMPDIR)" -lRblas
C_VISIBILITY = 
CC = $(BINPREF)gcc
CFLAGS = -O3 -Wall $(DEBUGFLAG) -std=gnu99
CPICFLAGS = 
CPPFLAGS =
CXX = $(BINPREF)g++
CXXCPP = $(CXX) -E
CXXFLAGS = -O2 -Wall $(DEBUGFLAG)
CXXPICFLAGS = 
DYLIB_EXT = .dll
DYLIB_LD = $(DLL)
DYLIB_LDFLAGS = -shared
DYLIB_LINK = $(DYLIB_LD) $(DYLIB_LDFLAGS) $(LDFLAGS)
ECHO = echo
ECHO_C = 
ECHO_N = -n
ECHO_T = 
F77 = $(BINPREF)gfortran
F77_VISIBILITY = 
FC = $(BINPREF)gfortran
FCFLAGS = -O3 $(DEBUGFLAG)
FFLAGS = -O3 $(DEBUGFLAG)
FLIBS = -lgfortran
FCPICFLAGS =
FPICFLAGS = 
FOUNDATION_CPPFLAGS =
FOUNDATION_LIBS =
JAR =
JAVA = 
JAVAC = 
#JAVA_HOME = 
# JAVA_LD_LIBRARY_PATH = 
JAVA_LIBS = -L"$(JAVA_HOME)/bin/client" -ljvm -ljvm-w32
JAVA_CPPFLAGS = -I"$(JAVA_HOME)/../include" -I"$(JAVA_HOME)/../include/win32"
LAPACK_LIBS = -L"$(R_HOME)/$(IMPDIR)" -lRlapack
## we only need this is if it is external, as otherwise link to R
LIBINTL=
LIBM = -lm
LIBR = -L"$(R_HOME)/$(IMPDIR)" -lR
LIBS =  -lm
## needed by R CMD config
LIBnn = lib
LIBTOOL =
LDFLAGS =
# don't have this: would 'mkdir -p' do?
# MKINSTALLDIRS = $(R_HOME)/bin/mkinstalldirs
OBJC = $(CC)
OBJCFLAGS = -O2
OBJC_LIBS = -lobjc
OBJCXX =
R_ARCH = 
RANLIB = $(BINPREF)ranlib
SAFE_FFLAGS = -O3 -ffloat-store
SED = sed
## it seems some makes (but not ours) get upset if SHELL is set.
# SHELL = /bin/sh
SHLIB_CFLAGS = 
SHLIB_CXXFLAGS = 
SHLIB_CXXLD = $(CXX)
SHLIB_CXXLDFLAGS = -shared
SHLIB_EXT = .dll
SHLIB_FCLD = $(FC)
SHLIB_FCLDFLAGS = -shared
SHLIB_FFLAGS = 
SHLIB_LD = $(CC)
SHLIB_LDFLAGS = -shared
SHLIB_LIBADD = 
SHLIB_LINK = $(SHLIB_LD) --export-all-symbols $(SHLIB_LDFLAGS) $(LDFLAGS)
STRIP_LIBS = $(BINPREF)strip --strip-unneeded
STRIP_STATIC_LIBS = $(BINPREF)strip --strip-debug
TCLTK_CPPFLAGS = -I "$(TCL_HOME)/include" -DWin32
TCLTK_LIBS = -L"$(TCL_HOME)/bin" -ltcl$(TCL_VERSION) -ltk$(TCL_VERSION)

STATIC_LIBR =

R_XTRA_CFLAGS =
R_XTRA_CPPFLAGS = -I"$(R_HOME)/include"
R_XTRA_CXXFLAGS = 
R_XTRA_FFLAGS = 

ALL_CFLAGS = $(R_XTRA_CFLAGS) $(PKG_CFLAGS) $(CPICFLAGS) $(SHLIB_CFLAGS) $(CFLAGS)
ALL_CPPFLAGS = $(R_XTRA_CPPFLAGS) $(PKG_CPPFLAGS) $(CPPFLAGS) $(CLINK_CPPFLAGS)
ALL_CXXFLAGS = $(R_XTRA_CXXFLAGS) $(PKG_CXXFLAGS) $(CXXPICFLAGS) $(SHLIB_CXXFLAGS) $(CXXFLAGS)
ALL_OBJCFLAGS = $(PKG_OBJCFLAGS) $(CPICFLAGS) $(SHLIB_CFLAGS) $(OBJCFLAGS)
ALL_OBJCXXFLAGS = $(PKG_OBJCXXFLAGS) $(CXXPICFLAGS) $(SHLIB_CXXFLAGS) $(OBJCXXFLAGS)
ALL_FFLAGS = $(R_XTRA_FFLAGS) $(PKG_FFLAGS) $(FPICFLAGS) $(SHLIB_FFLAGS) $(FFLAGS)
ALL_LIBS = $(PKG_LIBS) $(SHLIB_LIBADD) $(LIBR) $(LIBINTL)

.SUFFIXES:
.SUFFIXES: .c .cc .cpp .C .f .f90 .f95 .m .mm .M .o .a .def .dll .exe .rc

.c.o:
	$(CC) $(ALL_CPPFLAGS) $(ALL_CFLAGS) -c $< -o $@
.c.d:
	@echo "making $@ from $<"
	@gcc -std=gnu99 -MM $(ALL_CPPFLAGS) $< > $@
.cc.o:
	$(CXX) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
.cpp.o:
	$(CXX) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
.C.o:
	$(CXX) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
.cc.d:
	@echo "making $@ from $<"
	@$(CXX) -M $(ALL_CPPFLAGS) $< > $@
.cpp.d:
	@echo "making $@ from $<"
	@$(CXX) -M $(ALL_CPPFLAGS) $< > $@
.C.d:
	@echo "making $@ from $<"
	@$(CXX) -M $(ALL_CPPFLAGS) $< > $@
.m.o:
	$(OBJC) $(ALL_CPPFLAGS) $(ALL_OBJCFLAGS) -c $< -o $@
.m.d:
	@echo "making $@ from $<"
	@gcc -MM $(ALL_CPPFLAGS) $< > $@
.mm.o:
	$(OBJCXX) $(ALL_CPPFLAGS) $(ALL_OBJCXXFLAGS) -c $< -o $@
.M.o:
	$(OBJCXX) $(ALL_CPPFLAGS) $(ALL_OBJCXXFLAGS) -c $< -o $@
.f.o:
	$(F77) $(ALL_FFLAGS) -c $< -o $@
.f95.o:
	$(FC) $(PKG_FCFLAGS) $(FCPICFLAGS) $(FCFLAGS) -c  $< -o $@
.f90.o:
	$(FC) $(PKG_FCFLAGS) $(FCPICFLAGS) $(FCFLAGS) -c  $< -o $@

%.exe:
	$(LINKER) $(LINKFLAGS) $($*-LINKFLAGS) -o $@ $^ $($*-LIBS) $(LIBS)

%.dll:
	@echo EXPORTS > $*.def
	@$(NM) $^ | $(SED) -n 's/^.* [BCDRT] _/ /p' >> $*.def
	$(SHLIB_LD) -shared $(DLLFLAGS) -o $@ $*.def $^ $(ALL_LIBS)
	@$(RM) $*.def

lib%.dll.a: %.def
	$(DLLTOOL) $(DLLTOOLFLAGS) $($*-DLLTOOLFLAGS) --dllname $*.dll --input-def $*.def --output-lib $@

%.a:
	@$(RM) $@
	$(BINPREF)$(AR) crs $@ $^

%.o: %.rc
	$(BINPREF)$(RESCOMP) $(RESFLAGS) -i $< -o $@
